package main

import (
	"fmt"
	"math/rand"
	"sort"
)

type Hero struct {
	Name string
	Age int
}

// 结构体的切片类型
type HexoSlice []Hero

func (h HexoSlice) Len() int {
	return len(h)
}

func (h HexoSlice) Less(i, j int) bool {
	return h[j].Age > h[i].Age
}

func (h HexoSlice) Swap(i, j int) {
	var temp = h[i]
	h[i] = h[j]
	h[j] = temp
}


// 使用系统提供的方法进行排序

func main() {
	//var slice_str = []int{0, -1, 4, 8, -3, 40}
	//
	//sort.Ints(slice_str)
	//fmt.Println(slice_str)

	var heros = HexoSlice{}
	for i := 0; i <= 10; i++ {
		hero := Hero{
			Name: fmt.Sprintf("name_%d", rand.Intn(100)),
			Age: rand.Intn(100),
		}
		heros = append(heros, hero)
	}

	sort.Sort(heros)

	for _, v := range heros  {
		fmt.Println(v)
	}


}
